//
//  YSPhotoCell.m
//  A_target
//
//  Created by sss on 2018/6/12.
//  Copyright © 2018年 kb210. All rights reserved.
//

#import "YSPhotoCell.h"
#import "YSPhotoBroser.h"
#define Width  [UIScreen mainScreen].bounds.size.width
#define Height  [UIScreen mainScreen].bounds.size.height
@implementation YSPhotoCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
   
    self.scrollView = [[UIScrollView alloc] init];
    self.scrollView.frame = CGRectMake(0, 0, Width, Height);
    self.scrollView.minimumZoomScale = 1;
    self.scrollView.maximumZoomScale = 2.0;
    self.scrollView.showsVerticalScrollIndicator = NO;
    self.scrollView.showsHorizontalScrollIndicator = NO;
    self.scrollView.delegate = self;
    [self.contentView addSubview:self.scrollView];
//    self.scrollView.backgroundColor=[UIColor whiteColor];
//    self.backgroundColor=[UIColor blackColor];
//    UITapGestureRecognizer *tap1=[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tap1:)];
//    tap1.numberOfTapsRequired=1;
 
//    [self.scrollView addGestureRecognizer:tap1];
    UITapGestureRecognizer *singleTapBackgroundView = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(singleTapBackgroundView:)];
    UITapGestureRecognizer *doubleTapBackgroundView = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(doubleTapBackgroundView:)];
    doubleTapBackgroundView.numberOfTapsRequired = 2;
    [singleTapBackgroundView requireGestureRecognizerToFail:doubleTapBackgroundView];
    [self addGestureRecognizer:singleTapBackgroundView];
    [self addGestureRecognizer:doubleTapBackgroundView];
}
-(void)singleTapBackgroundView:(UITapGestureRecognizer*)tap{
    YSPhotoBroser *browser=[self viewController:self];
    [UIView animateWithDuration:0.25 animations:^{
        browser.alpha=0;
    } completion:^(BOOL finished) {
        [browser removeFromSuperview];
    }];
}

-(void)doubleTapBackgroundView:(UITapGestureRecognizer*)tap{
    if (self.scrollView.zoomScale==1.0) {
        self.scrollView.zoomScale=2.0;
    }else{
        self.scrollView.zoomScale=1.0;
    }
}
- (YSPhotoBroser *)viewController:(UIView *)next;
{
    for (UIView *next = [self superview]; next; next = next.superview) {
        UIView *nextResponder = next;
        if ([nextResponder isKindOfClass:[YSPhotoBroser class]]) {
            return (YSPhotoBroser *)nextResponder;
        }
    }
    return nil;
}
-(void)setImg:(UIImage *)img{
    _img=img;
    [self.imageVeiw removeFromSuperview];
    self.imageVeiw = [[UIImageView alloc] initWithImage:img];
//    UITapGestureRecognizer *tap=[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tap)];
//    tap.numberOfTapsRequired=2;
//    self.imageVeiw.userInteractionEnabled=YES;
//    [self.imageVeiw addGestureRecognizer:tap];
    
    self.imageVeiw.contentMode = UIViewContentModeScaleAspectFit;
    self.imageVeiw.frame =[self setImage:self.imageVeiw];
    
    [self.scrollView addSubview:self.imageVeiw];
    //设置scroll的contentsize的frame
    self.scrollView.contentSize = self.imageVeiw.frame.size;
}
-(void)tap{
    
  
//         self.imageVeiw.transform=CGAffineTransformIdentity;
    self.scrollView.zoomScale=1.0;
   
    
    
}
//根据不同的比例设置尺寸
-(CGRect) setImage:(UIImageView *)imageView {
    
    CGFloat imageX = imageView.frame.size.width;
    CGFloat imageY = imageView.frame.size.height;
    CGRect imgfram;
    CGFloat CGscale;
    
    BOOL flx = (Width / Height) > (imageX / imageY);
    if(flx) {
        CGscale = Height / imageY;
        imageX = imageX * CGscale;
        imgfram = CGRectMake((Width - imageX) / 2, 0, imageX, Height);
        return imgfram;
        
    } else {
        CGscale = Height / imageX;
        imageY = imageY * CGscale;
        imgfram = CGRectMake(0, (Height - imageY) / 2, Width, imageY);
        return imgfram;
        
    }
    
}

//这个方法的返回值决定了要缩放的内容(只能是UISCrollView的子控件)
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
{
    return self.imageVeiw;
}
//控制缩放是在中心
- (void)scrollViewDidZoom:(UIScrollView *)scrollView {
    
    CGFloat offsetX = (scrollView.bounds.size.width > scrollView.contentSize.width)? (scrollView.bounds.size.width - scrollView.contentSize.width) * 0.5 : 0.0;
    
    CGFloat offsetY = (scrollView.bounds.size.height > scrollView.contentSize.height)? (scrollView.bounds.size.height - scrollView.contentSize.height) * 0.5 : 0.0;
    
    self.imageVeiw.center = CGPointMake(scrollView.contentSize.width * 0.5 + offsetX, scrollView.contentSize.height * 0.5 + offsetY);
    
}



@end
